Intro to Shiny

Learn how to make your own interactive web apps in R

Josh Cullen

July 25, 2024

What is Shiny?

Shiny is software for web app development that mitigates the need for knowledge of typical programming languages (e.g., JavaScript, HTML, and CSS) for web development.

  • All you need to know is how to code in R (or now Python!) to develop and deploy a web application
  • These web applications are often run:
    1. locally
    2. online
    3. as part of an R package

Why Shiny?

There are a wide number of reasons why you may be interested in creating a web app with Shiny:

  1. Data exploration
  1. Data collection
  1. Sharing results
  1. Decision support

Why Shiny?

Additionally, web apps are a good solution for many projects since they are:

  1. Free for end-users
  2. Cross-platform (e.g., different devices and operating systems)
  3. Easily accessible to many people without software installation or knowledge of R

How Shiny?

Like all web apps, Shiny can be broken down into its two major components:

  1. User Interface (UI or front-end)
  2. Server (or back-end)

How Shiny?

The UI is what all app users see and interact with, whereas the server is what does all the work behind the scenes.

library(shiny)

ui <- fluidPage(
  titlePanel("Old Faithful"),
  sidebarLayout(
    sidebarPanel = sidebarPanel(
      sliderInput(inputId = "bins", label = "Number of bins:",
            min = 1, max = 50, value = 30)
    ),
    mainPanel = mainPanel(
      plotOutput("distPlot")
    )
  )
)
1
Define title of app
2
Add an input widget
3
Add the output product
server <- function(input, output, session) {
  
     output$distPlot <- renderPlot({
      # generate bins based on input$bins from ui.R
      wait <- faithful[, 2] 
      bins <- seq(min(wait), max(wait), length.out = input$bins + 1)
      
      # draw the histogram with the specified number of bins
      hist(wait, breaks = bins, col = 'darkgray', border = 'white')
   })
     
}
4
Store output product
5
Define variable influenced by input
6
Create resulting plot

How Shiny?

How Shiny?

The third important component of Shiny apps is reactive expressions.

  • Reactive objects automatically update the state of the app to produce changes to outputs that reflect changes to inputs

Photo credit: reactlog

Sharing Shiny apps

Apps can be shared using:

  • a set of files (e.g., app.R, data.csv) run locally in R
  • deployed on an online server (e.g., shinyapps.io has ‘Free’ plan)
  • deployed as a static file online (e.g., GitHub pages using shinylive)

If deployed online, apps can even be embedded on separate websites

Green turtle example

Additional resources

Your turn

Let’s do some hands-on examples to practice building and sharing Shiny apps from the ground up